package com.gangwangchina.android.contactlib.model;

import java.util.HashMap;
import java.util.Map;

import com.gangwangchina.android.contactlib.ContactsInterface;

import android.net.Uri;
import android.provider.ContactsContract.CommonDataKinds;


public class TelephoneNumber extends DataElement {

	public static final String NUMBER = CommonDataKinds.Phone.DATA;
	public static final String TYPE = CommonDataKinds.Phone.TYPE;
	public static final String LABEL = CommonDataKinds.Phone.LABEL;
	public static final String CONTENT_ITEM_TYPE = CommonDataKinds.Phone.CONTENT_ITEM_TYPE;
	public static final Uri FILTER_SEARCH = CommonDataKinds.Phone.CONTENT_FILTER_URI;

	public String number, label;
	public int type;

	/**
	 * Creates a new Telephone Number that is not (yet) present in the database
	 * 
	 * @param number
	 * @param type
	 * @param label
	 * @return
	 */
	public static TelephoneNumber newTelephoneNumber(String number, int type,
			String label) {
		return new TelephoneNumber(number, type, label);
	}

	/**
	 * Creates an object representation of a Telephone Number that is present in
	 * the database
	 * 
	 * @param pcId
	 * @param number
	 * @param type
	 * @param label
	 * @return
	 */
	public static TelephoneNumber recoverTelephoneNumber(long pcId,
			String number, int type, String label) {
		return new TelephoneNumber(pcId, number, type, label);
	}

	private TelephoneNumber(long pcId) {
		super(pcId, CONTENT_ITEM_TYPE);
	}

	private TelephoneNumber(long pcId, String number, int type, String label) {
		this(pcId);
		this.number = number;
		this.type = type;
		this.label = label;
	}

	private TelephoneNumber(String number, int type, String label) {
		this(ContactsInterface.NO_ID, number, type, label);
	}

	@Override
	public String toString() {
		return new StringBuilder().append('(').append(number).append(',')
				.append(type).append(',').append(label).append(')').toString();
	}

	@Override
	public Map<String, Object> getDataColumns() {				
		Map<String, Object> datas = new HashMap<String, Object>();
		
		datas.put(NUMBER, number);
		datas.put(LABEL, label);
		datas.put(TYPE, String.valueOf(type));
		return datas;
	}

	
	@Override
	public boolean setProperty(String key, Object value) {
		if (key.equals(NUMBER))
			number = (String) value;
		else if (key.equals(TYPE))
			type = new Integer((Integer) value);
		else if (key.equals(LABEL))
			label = (String) value;
		else
			return super.setProperty(key, value);
		return true;
	}

	@Override
	public Object getProperty(String key) {
		if (key.equals(NUMBER))
			return number;
		else if (key.equals(TYPE))
			return String.valueOf(type);
		else if (key.equals(LABEL))
			return label;
		else
			return super.getProperty(key);

	}
}